<template>
  <div class="retention">
    <div class="content">
      <el-main
        v-loading="loading"
        style="margin: 20px auto; width: 100%; align-items: center"
      >
        <el-table
          border
          :fit="true"
          :header-cell-style="{
            color: '#333',
            fontFamily: 'MicrosoftYaHeiUI',
            fontWeight: 500,
            fontSize: '16px',
          }"
          :data="tableData.slice((currentPage - 1) * pageSize,currentPage * pageSize)"
        >
          <el-table-column
            prop="time" 
            sortable 
            label="时间"
          ></el-table-column>
          <el-table-column
            prop="dau" 
            label="DAU" 
            sortable
          ></el-table-column>
          <el-table-column
            prop="next_day_retention_rate"
            sortable
            label="次日留存率"
          ></el-table-column>
          <el-table-column
            prop="three_day_retention_rate"
            sortable
            label="三日留存率"
          ></el-table-column>
          <el-table-column
            prop="seven_day_retention_rate"
            sortable
            label="七日留存率"
          ></el-table-column>
          <el-table-column
            prop="fifteen_day_retention_rate"
            sortable
            label="十五日留存率"
          ></el-table-column>
        </el-table>
        <pageNation
          :total="tableData.length"
          :page-size="pageSize"
          @pageChange="pageChange"
          :page_index="currentPage"
        />
      </el-main>
    </div>
  </div>
</template>
<script>
import pageNation from "../../components/pagenation/pageNation";
export default {
  name: "Retention",
  components: { pageNation },
  data() {
    return {
      tableData: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
    };
  },
  created() {
    this.getStatTime();
  },
  methods: {
    //分页
    pageChange(item) {
      this.currentPage = item.page_index;
      this.pageSize = item.page_limit;
    },
    formatDate(now) {
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      return year + "-" + month + "-" + date;
    },
    //获取用户最早注册的时间
    getStatTime() {
      return new Promise((resolve, reject) => {
        var endDate = parseInt(new Date().getTime() / 1000);
        var startDate = 0;
        var list = [];
        this.loading = true;
        this.$http.post("/api/retention/getfirstday",{
          endDate:endDate,
        }).then((res) => {
            res.data.forEach((item) => {
              startDate = item.register_time;
            });
            var day1 = this.formatDate(new Date(startDate * 1000));
            var start_time=new Date(Date.parse(day1.replace(/-/g, "/"))).getTime() / 1000;
            var day = Math.floor((endDate - start_time) / (24 * 3600));
            for (let i = 0; i < day; i++) {
              var parms = start_time + i * (24 * 3600);
              this.search(parms).then((value) => {
                list.push(value);
                list.sort((a, b) => {
                  return a.time < b.time ? 1 : -1;
                });
                this.tableData = list;
                this.loading = false;
              });
            }
          });
      });
    },
    search(parms) {
      //查询留存率信息
      return new Promise((resolve, reject) => {
        this.$http.post("/api/retention/getretention", {
          start_time: parms,
        }).then((res) => {
          resolve(res.data);
        }).catch((err) => {
          reject(err);
        });
      });
    },
  },
};
</script>
<style scoped>
.retention {
  width: 100%;
  height: auto;
  background-color: #fff;
}
</style>